/*
 * tcpServer.h
 *
 *  Created on: 2015年10月23日
 *      Author: linchen
 */




#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <pthread.h>
#include <list>
#include <string>
#include "ActiveObject.h"

class ClientConnection;

class TcpServer : public ActiveObject
{
public:
		virtual ~TcpServer();
		TcpServer(int port);
		void start();
		void stop();
		int  sendBroadcastMessage(const char *buf, int length);
		typedef std::list<ClientConnection *>  ClientList_t;
		ClientList_t m_clientList;

private:
		pthread_mutex_t  m_mutex;                 // 互斥锁
		bool m_runing;
		int m_socket_fd;
		int m_port;
		int run ();
		void init();
};


class ClientConnection
{
public:
    ClientConnection (int fd);
    virtual ~ ClientConnection ();

    int readmessage (char *buf, int len);
    int sendmessage (int size, const char * msg);
    std::string m_msg;
    std::string m_msgbuf;
    int m_connectfd;

private:
    pthread_mutex_t  m_mutex;                 // 互斥锁

};

#endif
